package com.tianpengtech.front.web.count;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.task.TaskSource;
import com.tianpengtech.common.model.task.TaskWh;
import com.tianpengtech.common.util.IpKit;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.front.web.common.interceptor.PassportInterceptor;
import com.tianpengtech.front.web.task.TaskCountService;
import com.tianpengtech.front.web.task.TaskService;

@Clear({PassportInterceptor.class})
public class TaskCountController extends Controller{

	private TaskService service = new TaskService();
	private TaskCountService tcService = new TaskCountService();
	public void index(){
		String ucode = getPara("ucode","c03210fe012c436fb5a1a5eb308d430e");
		String code = getPara("code","");
		Fair fair = tcService.getFairInfoByCode(ucode);
		int id = getParaToInt(Task.ID,0);
//		List<TaskSource> source = service.findSourceByTask(id);
//		setAttr("sources", source);
		Task task= Task.dao.findById(id);
		String template = getPara("type","default");
		setAttr("template",template);
		setAttr("fair",fair);
		if(task!=null){
			String webUrl = task.getStr(Task.WEB_URL);
			if(task!=null){
				if(webUrl!=null&&!webUrl.startsWith("http")){
					webUrl = "http://"+webUrl;
					task.set(Task.WEB_URL, webUrl);
				}
			}
			
			setAttr("task",task);
			setAttr("ucode",ucode);
			setAttr("code",code);
			//setSessionAttr("nickName",tcService.gernerateNickName(IpKit.getRealIp(getRequest())));
			List<TaskSource> source = service.findSourceByTask(task.getInt(Task.ID));
			setAttr("source", source);
			
			
		}
		renderTemplate("task-count.html");
	}
	
	public void getTaskWhInfo(){
		String ucode = getPara("ucode","c03210fe012c436fb5a1a5eb308d430e");
		Record record= tcService.getTaskClickInfo(ucode);
		
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("click_num", 0);
		param.put("reason", "看过来，这里最精彩，跟我一起做像样的网红!!!");
		if(record!=null){
			if(record.getLong(TaskWh.CLICK_NUM)>0){
				param.put("click_num",record.getLong(TaskWh.CLICK_NUM));
			}
			if(StringUtils.isNotEmpty(record.getStr(TaskWh.REASON))){
				param.put("reason",record.getStr(TaskWh.REASON));
			}
		}
		renderJson(JsonUtil.printObject(param));
	}
	
	
	public void getFairInfo(){
		String code = getPara("ucode","c03210fe012c436fb5a1a5eb308d430e");
		Fair fair = tcService.getFairInfoByCode(code);
		Map<String,Object> param = new HashMap<String,Object>();
		if(fair!=null){
			param.put("nickName", fair.getStr(Fair.NICK_NAME));
			param.put("avatarUrl", fair.getStr(Fair.PHOTO));
			param.put("recommend", tcService.getRecommandReason(fair.getLong(Fair.ID)));
		}else{
			param.put("nickName","网红昵称");
			param.put("avatarUrl","/upload/avatar/x.jpg");
			param.put("recommend", "看过来，这里最精彩，跟我一起做像样的网红!!!");
		}
		renderJson(JsonUtil.printObject(param));
	}
	
	
	
	public void getTaskPicList(){
		int id = getParaToInt(Task.ID,0);
		List<TaskSource> source = service.findSourceByTask(id);
		List<String> picList = new ArrayList<String>();
		for (TaskSource each : source) {
			picList.add(each.getStr(TaskSource.SOURCE));
		}
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("picList", picList);
		renderJson(JsonUtil.printObject(param));
	}
	
	public void saveComment(){
		String fromUserUrl = getPara("fromUserUrl","");
		String fromUserNickName = getPara("fromUserNickName","");
		String toUserNickName = getPara("toUserNickName","");
		String toUserNickUrl = getPara("toUserNickUrl","");
		String ucode = getPara("ucode","c03210fe012c436fb5a1a5eb308d430e");
		int taskId = getParaToInt("taskId",0);
		if(taskId>0){
			String comments = getPara("comments","");
			Fair fair = tcService.getFairInfoByCode(ucode);
			int fair_id = (fair==null)?0:fair.getInt(Fair.ID);
			tcService.saveComments(IpKit.getRealIp(getRequest()),taskId,fromUserNickName,fromUserUrl,toUserNickName,toUserNickUrl,comments,fair_id);
			renderJson(JsonUtil.printSuccess("留言成功"));
		}else{
			renderJson(JsonUtil.printError("当前任务未创建，不允许评论!"));
		}
		
	}
	
	
	public void getComment(){
		int taskId = getParaToInt("taskId",0);
		List<Map<String,Object>> commentList = new ArrayList<Map<String,Object>>();
		String ucode = getPara("ucode","c03210fe012c436fb5a1a5eb308d430e");
		Fair fair = tcService.getFairInfoByCode(ucode);
		int fair_id = (fair==null)?0:fair.getInt(Fair.ID);
		List<Record> comments = tcService.getCommentsByTask(taskId,fair_id);
		for (Record each : comments) {
			Map<String,Object> user = new HashMap<String,Object>();
			Map<String,Object> toUser=null;
			Map<String,Object> comment = new HashMap<String,Object>();
			user.put("userId",each.get("id"));
			user.put("nickName",each.get("from_user_nickname"));
			user.put("avatarUrl",each.get("from_user_url"));
			
			if(StringUtils.isNotEmpty(each.getStr("to_user_nickname"))){
				toUser = new HashMap<String,Object>();
				toUser.put("userId",each.get("id"));
				toUser.put("nickName",each.get("to_user_nickname"));
				toUser.put("avatarUrl",each.get("to_user_url"));
			}
			
			
			comment.put("id",each.get("id"));
			comment.put("comments",each.get("comment"));
			comment.put("fromUser",user);
			comment.put("toUser",toUser);
			commentList.add(comment);
		}
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("commentList", commentList);
		renderJson(JsonUtil.printObject(param));
	}
	
	
	public void getUserInfoFromWeixin(){
		
		String code = getPara("code","");
		String ip = IpKit.getRealIp(getRequest());
		Map<String,Object> wxUser = getSessionAttr("wxuser");
		if(wxUser==null){
			wxUser = tcService.getUserInfoFromWeixin(code,ip);
			setSessionAttr("wxuser", wxUser);
		}
		renderJson(JsonUtil.printObject(wxUser));
	}


	public void gernerateNickName(){
		String ip= IpKit.getRealIp(getRequest());
		renderJson(JsonUtil.printObject(tcService.gernerateNickName(ip)));
	}

	public void test(){
		renderTemplate("weixin-code.html");
	}
	
	public void commentModal(){
		renderTemplate("comment-modal.html");
	}
}
